$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			isCollapse: true
			,queryIng : false//正在查询
			,queryData:{activeFlag:'1',page:1,rows:10}//查询条件
			,datagrid:[]//数据列表数据
			,totalNum:0//分页控件配置
			,data:[]
			,defaultCheckedKeys:[]
			,addData:{status:1}
			,selectedRow:null
			,isEdit:false
			,rules:{
				name: [{ required: true, message: '请输入活动名称', trigger: 'change' }]
				,password: [{ required: true, message: '请输入活动名称', trigger: 'change' }]
				,nickName: [{ required: true, message: '请输入活动名称', trigger: 'change' }]
				,email: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }]
			}
			,dialogConfig:{
				formLabelWidth:'120px'
				,dialogFormVisible:false
				,fileDialogVisible:false
			}
			,trees:[]
			,checkedTrees:[]
			,loading:false
			,fileLoading:false
		},
		methods: {
			init:function () {
				var vm = this
				vm.query();
			}
			,query:function () {
				var vm = this
				if(vm.queryIng){
					return;
				}
				vm.queryIng = true;
				$.ajax({
					url :staticURL+'/basic/userInfo/datagrid',
					data : vm.queryData,
					dataType : 'json',
					success : function(json) {
						app.datagrid = json.rows;
						app.totalNum = json.total;
						app.queryIng = false;
						app.selectedRow = null;
					}
				});
			},
			search:function(){
				app.queryData.page=1;
				app.query();
			}
			,addItem:function(){
				app.openDialog();
				app.addData.status='1';
			}
			,deleteItem:function(row){
				
				app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
							url : staticURL+"/basic/userInfo/delete?ids="+row.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				return;
				
			}
			,editItem:function(row){
				app.openDialog(row);
			}
			,handleSizeChange:function(val){
				app.queryData.rows=val;
				app.query();
			}
			,handleCurrentChange:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handlePrevClick:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handleNextClick:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handleTreeCheckChange:function(data){
				app.queryData.parentCode = data.id;
				app.query();
			}
			,resetForm:function(formName) {
				app.$refs[formName].resetFields();
		    }
			,submitForm:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						//验证通过
						
						var nodes = app.$refs.tree.getCheckedNodes();
						
						var ids = [];
						
						for(var i=0;i<nodes.length;i++){
							
							if(!nodes[i].obj){
								
								ids.push(nodes[i].id);
							}
							
						}
						
						app.addData.resourceIds = ids.join(',');
						
						$.ajax({
							url : app.saveUrl,
							data : app.addData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.dialogFormVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			}
			,openDialog:function(param){
				
				var userId = null;
				
				if(param){
					app.isEdit = true;
					
					app.addData = JSON.parse(JSON.stringify(param)); 
					
					userId = app.addData.id;
					
				}else{
					app.isEdit = false;
					app.addData = {orderNum:1};
				}
				
				this.loadResources(userId);
				
				//显示dialog
				app.dialogConfig.dialogFormVisible=true;
				//重置form
				if(app.$refs['dialogForm']){
					app.$refs['dialogForm'].resetFields();
				}
			}
			,closeDialog:function(){
				app.dialogConfig.dialogFormVisible=false;
				app.resetForm('dialogForm')
			}
			,datagridSelect:function(row, event, column){
				app.selectedRow = row;
			}
			,formatterStatus:function(row, column, cellValue, index){
				if(row.status == '0'){
					return '禁用';
				}else if(row.status == '1'){
					return '启用';
				}
				return '未知'
			}
			,getSelectedUser:function(){
				return this.selectedRow
			}
			,addDemo:function(){
				layer.open({
				  type: 2, 
				  title:false,
				  area:['100%','100%'],
				  offset:["0px","0px"],
				  icon:0,
				  closeBtn: 0,
				  content: staticURL+'/basic/userInfo/goDemo',
				  end:function(){
				  }
				});
			}
			,printPreview:function(){
				var url = staticURL+'/ureport/preview?_u=file:userinfo_list.ureport.xml';
				if(this.queryData.name){
					url = url+"&name="+this.queryData.name;
				}
				if(this.queryData.nickName){
					url = url+"&nickName="+this.queryData.nickName;
				}
				window.open(url);
			}
			,printPreviewDemo:function(){
				var url = staticURL+'/ureport/preview?_u=file:demo.ureport.xml';
				window.open(url);
			}
			,loadResources:function(id){
			
				$.ajax({
					url : staticURL+'/basic/userResourceAction/findResource',
					data:{
						userId:id
					},
					dataType : 'json',
					success : function(r) {
						
						app.trees = r;
						
						var list = [];
						
						for(var i=0;i<r.length;i++){
							
							if(r[i].checked=="1"){
								list.push(r[i].id);
							}
							
							var children = r[i].children;
							
							for(var j=0;j<children.length;j++){
								
								if(children[j].checked=="1"){
									list.push(children[j].id);
								}
							}
						}
						
						app.checkedTrees = list;
						
					}
				});
				
			}
			,exportExcel:function(){
				window.open(staticURL+'/basic/userInfo/exportNew');
			}
			,importExcel:function(index,data){
		    	this.dialogConfig.fileDialogVisible = true
		    }
			,uploadFileSuccess:function(response, file, fileList){
		    	if(response.success){
		    		app.dialogConfig.fileDialogVisible = false;
		    		app.$notify({showClose: true,
		    			message: '上传成功',
		    			type: 'success',
		    			position: 'bottom-right'});
		    		app.query();
		    	}else{
		    		app.$notify({showClose: true,
		    			message: response.msg,
		    			type: 'error',
		    			position: 'bottom-right'});
		    	}
		    	app.$refs['fileUploader'].clearFiles();
		    	this.fileLoading = false;
		    }
		    ,uploadFileError:function(){
		    	app.$notify({showClose: true,
			          message: '上传失败',
			          type: 'error',
			          position: 'bottom-right'});
		    	app.$refs['fileUploader'].clearFiles();
		    	this.fileLoading = false;
		    }
		    ,doDownload:function(index,data){
		    	window.open(staticURL+"/basic/uploadFile/findAttach?id="+data.id);
		    }
		    ,beforeUpload:function(file){
		    	//文件大小kb
		    	var fileSize = file.size / 1024
		    	console.log("fileSize "+fileSize);
		    	if(fileSize > 500){
		    		this.$message({  
                        message: '上传文件大小不能超过 500KB!',  
                        type: 'warning'  
                    });  
		    		return false;
		    	}
		    	this.fileLoading = true;
		    }
		},
		computed:{
			saveUrl:function(){
				if(app.isEdit){
					return staticURL+'/basic/userInfo/edit'
				}else{
					return staticURL+'/basic/userInfo/add'
				}
			},
			columnWidth:function(){

				let w = $(window).width();   

				w = w - 48-48-50-150;
				
				return Math.floor(w/5);
			}
		}
		,created: function () {
			this.init();
			window._getSelectedUser = this.getSelectedUser
		},
		updated:function(){
		}
	});
});